type formatSourceType = {
  BYTE: number,
  KB: number,
  MB: number,
  GB: number
}

const formatValue = (size: string | number, targetUnit?: string) => {
    const source:formatSourceType  = {
      BYTE: 1,
      KB: 1024,
      MB: 1024 * 1024,
      GB: 1024 * 1024 * 1024,
    };
    let unit: string; // 传入的数字中带的单位
    let numReg = /^\d+/g;
    let unitReg = /[a-z|A-Z]+$/g;
    let result: string; // 结果值
    let transfer: number; //用来过渡的变量转换
    if (typeof size === "string") {
      unit = size.match(unitReg)?.toString().toUpperCase(); //将size中的单位转成大写
      size = size.match(numReg)?.toString();
      size = size && Number(size)
    }

    // 先把所有传入的数据转化成字节
    // Object.keys(source).forEach((item, i) => {
    //   if (unit === item) {
    //     size = size * source[item];
    //   }
    // });

    if(unit && typeof size==='number') {
        size = size * source[unit]
    }
    

    // 如果有目标单位
    if (targetUnit && typeof size==='number') {
      targetUnit = targetUnit.toUpperCase(); //将目标单位转成大写
    //   Object.keys(source).forEach((item, i) => {
    //     if (targetUnit === item) {
    //       result = size / source[item] + targetUnit;
    //     }
    //   });
      result = size / source[targetUnit] + targetUnit
    }

    // 如果没有目标单位
    if (!targetUnit ) {
      Object.keys(source).forEach((item, i) => {
        if(typeof size === 'number'){

          transfer = size / source[item];
          if (transfer > 1 && transfer <= 1024) {
            result = transfer + item;
          }
        }
      });
    }
    return result;
  };

  console.log(formatValue("10259874KB"));
  console.log(formatValue(10259874));
  console.log(formatValue("10259874Gb", "KB"));
  export {}